﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PIT.Labs.SharePoint.Extensions
{
    /// <summary>
    /// This class contains "<a href="http://msdn.microsoft.com/en-us/library/bb383977%28v=vs.90%29.aspx">Extension Methods</a>"
    /// for the <seealso cref="T:Microsoft.SharePoint.SPItemEventPropertiesExtension"/> class.
    /// </summary>
    public static class SPItemEventPropertiesExtension
    {
        /// <summary>
        /// Gets the value within the collection by its field identifier.
        /// </summary>
        /// <param name="spItemEventProperties">The instance of the object.</param>
        /// <param name="fieldID">The GUID of the field.</param>
        /// <returns>If the field value can be found, the value is returned as <see cref="String"/>, otherwise NULL.
        /// </returns>
        [CLSCompliantAttribute(false)]
        public static String GetAfterProperty(this SPItemEventProperties spItemEventProperties, Guid fieldID)
        {
            String foundValue = null;
            SPList associatedList = spItemEventProperties.List;
            if (associatedList != null && fieldID != null)
            {
                using (SPSite siteCollection = new SPSite(associatedList.ParentWeb.Url))
                {
                    using (SPSite elevatedSite = siteCollection.GetElevatedSite())
                    {
                        using (SPWeb elevatedWeb = elevatedSite.OpenWeb(associatedList.ParentWeb.ID))
                        {
                            SPList elevatedAssociatedList = elevatedWeb.Lists[associatedList.ID];
                            if (elevatedAssociatedList.Fields.Contains(fieldID))
                            {
                                SPField field = elevatedAssociatedList.Fields[fieldID];
                                String internalFieldName = field.InternalName;
                                foundValue = spItemEventProperties.AfterProperties[internalFieldName] as String;
                            }
                        }
                    }
                }
            }
            return foundValue;
        }

        /// <summary>
        /// Sets the value within the collection by its field identifier.
        /// </summary>
        /// <param name="spItemEventProperties">The instance of the object.</param>
        /// <param name="fieldID">The GUID of the field.</param>
        /// <param name="value">The value of property.</param>
        [CLSCompliantAttribute(false)]
        public static void SetAfterProperty(this SPItemEventProperties spItemEventProperties, Guid fieldID, String value)
        {
            if (fieldID == null)
                throw new ArgumentNullException("fieldID");
            SPList associatedList = spItemEventProperties.List;
            if (associatedList != null)
            {
                using (SPSite siteCollection = new SPSite(associatedList.ParentWeb.Url))
                {
                    using (SPSite elevatedSite = siteCollection.GetElevatedSite())
                    {
                        using (SPWeb elevatedWeb = elevatedSite.OpenWeb(associatedList.ParentWeb.ID))
                        {
                            SPList elevatedAssociatedList = elevatedWeb.Lists[associatedList.ID];
                            if (elevatedAssociatedList.Fields.Contains(fieldID))
                            {
                                SPField field = elevatedAssociatedList.Fields[fieldID];
                                String internalFieldName = field.InternalName;
                                spItemEventProperties.AfterProperties[internalFieldName] = value;
                            }
                            else
                            {
                                throw new ArgumentException("Field with GUID \""
                                    + fieldID.ToString("D", System.Globalization.CultureInfo.InvariantCulture) + "\" not found.", "fieldID");
                            }
                        }
                    }
                }
            }

        }

    }
}
